package org.example.Frame;

import org.example.Util.FileUtil;
import org.example.Pojo.User;
import org.example.implement.MyFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;

public class ImageChoose extends JFrame implements MyFrame{
    private int difficulty;
    private JButton[] buttons=new JButton[3];
    private Vector<File> imgs;//图片数组
    private int choose;//选择
    private JTextField Time;//时间设定
    private JLabel Timer;//时间标签
    private JLabel help;//输入说明
    private User player;//玩家信息
    //初始化界面
    public ImageChoose(int difficulty,User player){
        //初始化界面
        this.setSize(1200,800);
        this.setTitle("图片选择界面");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //初始化变量
        this.difficulty=difficulty;
        imgs=new FileUtil().Readimgs();
        this.player=player;
        //初始化组件
        JPanel panel=new JPanel();
        draw(panel);
        this.add(panel);
        this.setVisible(true);
    }
    //绘画板
    @Override
    public void draw(JPanel panel){
        panel.setLayout(null);
        for(int i=0;i<3;i++) {
            buttons[i] = new JButton();
            buttons[i].setIcon(new ImageIcon(imgs.get(i + difficulty * 3).getAbsolutePath()));//添加图片
            buttons[i].setBounds(200 + (i * 300), 300, 180, 180);//设置坐标
            panel.add(buttons[i]);
        }
            //标签初始化
            Timer=new JLabel();
            Timer.setText("限制的时间:");
            Timer.setBounds(420,600,80,20);
            panel.add(Timer);
            //时间填写
            Time=new JTextField();
            Time.setBounds(500,600,200,20);
            panel.add(Time);
            //说明
            help=new JLabel();
            help.setText("若不填写则表示不限时模式，按照分钟填写设置时间，例如:填写\"1\"则表示限时一分钟");
            help.setBounds(350,640,500,20);
            panel.add(help);
            //按钮点击逻辑
            for(int i=0;i<3;i++){
                int finalI = i;
                buttons[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        choose= finalI;
                        close();
                    }
                });
            }
    }

    //关闭界面
    @Override
    public void close(){
        if(Time.getText().isEmpty()){
            this.setVisible(false);
            new Game(difficulty,imgs.get(choose+difficulty*3),0,player);
        }
        else{
            int time=1;
            System.out.println(Time.getText());
            //time= Integer.parseInt(Time.getText());
            try {
                System.out.println(Time.getText());
                time= Integer.parseInt(Time.getText());//暂存参数
                if(time==0){
                    JOptionPane.showMessageDialog(new JOptionPane(),"时间输入有误，请重新输入","警告",JOptionPane.WARNING_MESSAGE);
                }
                else {
                    this.setVisible(false);
                    new Game(difficulty,imgs.get(choose+difficulty*3),time,player);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(new JOptionPane(),"时间输入有误，请重新输入","警告",JOptionPane.WARNING_MESSAGE);
            }
        }
    }
}
